function Promise(fn){
    const missions = [];  //待执行队列
    var value = null;
    var state = 'pending';
    var next_resolve = null;

    //执行传入的方法
    fn(resolve);

    //当传入的方法中调用resolve(value)时，异步执行mission
    function resolve(_return_value){
        value = _return_value;
        state = 'fulfilled';
        setTimeout(()=>{
            missions.forEach(mission=>{
                handle(mission);
            })
        }, 0);
    }

    //执行then方法时，将传入的方法加入missions，等待resolve触发。
    this.then = function(mission){
        var fn = function(resolve){
            next_resolve = resolve;
            if(state === 'pending'){
                missions.push(mission)
            }else{
                handle(mission);
            }
        }
        return new Promise(fn);
    }

    function handle(mission){
        const result = mission(value);
        //当处理结果为Promise对象时，将next_resolve推入待执行队列
        if(result && (typeof result == 'object' || typeof result == 'function')){
            if(result.then && typeof result.then == 'function'){
                result.then(next_resolve);
            }
        }else{
            next_resolve(result);
        }
    }
}